package com.jmebananas.math;

import com.jme.math.FastMath;

/**
 * SpringFloatController
 * 
 * @author Glenn Powell
 * 
 */
public class SpringFloatController implements FloatController {
	
	private float value;

	private float springK;

	private float dampingK;

	private float velocity;


	public SpringFloatController(float springK) {
		this(springK, (float)(2 * Math.sqrt(springK)));
	}

	public SpringFloatController(float springK, float dampingK) {
		this.value = 0;
		this.springK = springK;
		this.dampingK = dampingK;
		this.velocity = 0;
	}

	public boolean update(float time) {
		float v = value;
		v = v * -springK - velocity * dampingK;
		velocity += v * time;
		if (Float.isNaN(velocity) || Float.isInfinite(velocity))
			velocity = 0;
		value += velocity * time;
		return Math.abs(velocity) > FastMath.FLT_EPSILON; 
	}

	public float getDampingK() {
		return dampingK;
	}

	public void setDampingK(float dampingK) {
		this.dampingK = dampingK;
	}

	public float getValue() {
		return value;
	}

	public void setValue(float value) {
		this.value = value;
	}

	public float getSpringK() {
		return springK;
	}

	public void setSpringK(float springK) {
		this.springK = springK;
	}

	public float getVelocity() {
		return velocity;
	}

	public void setVelocity(float velocity) {
		this.velocity = velocity;
	}

}
